<?php
/**
 * Class to handle mysql database connections and communication
 *
 * This DbMysql class supplies the needed database methods for
 * communication with the mysql database.
 *
 * @copyright  2009 A Memory Tree
 * @license    A Memory Tree License
 * @version    $Id:$
 * @link       http://my-trac.assembla.com/amemorytree/ticket/38
 * @since      File available since Release 0.1
 */
/*. require_module 'mysql'; .*/
/**
 * Handle database connections and communication
 *
 * The purpose of this db class is to supply MySql specific
 * database communication techniques.
 *
 * These class methods are planned to be deprecated over time,
 * when other techniques will be used instead.
 *
 * @copyright  2009 A Memory Tree
 * @license    A Memory Tree License
 * @version    Release: @package_version@
 * @link       http://my-trac.assembla.com/amemorytree/ticket/38
 * @since      Class available since Release 0.1
 */
class DbMySql
{
    /**
     * Contains a link to the MySql database
     * @var resource
     */
    private $_link;
    /**
     * Connect to the database server
     *
     * @param string $server Path to the server, commonly 'localhost'
     * @param string $name   The username
     * @param string $pass   The password
     * @return resource      Link identifier on success, or FALSE on failure
     */
    function connect($server, $name, $pass)
    {
        $this->_link = @mysql_connect($server, $name, $pass);
        return $this->_link;
    }
    /**
     * Select a database from the server
     *
     * @param string $database The name of the database to be selected
     * @return boolean        Link identifier on success, or FALSE on failure
     */
    public function select($database)
    {
        return @mysql_select_db($database, $this->_link);
    }
    /**
     * Convert values so they are safe being used in an SQL string
     *
     * @param string $value The value to be escaped
     * @return string       The escaped value, or FALSE on error
     */
    public function escape($value)
    {
        return mysql_real_escape_string($value, $this->_link);
    }
    /**
     * Send a unique query to the database
     *
     * @param string $sql The SQL query to be sent to the database
     * @return resource   Either a resultset or true/false depending on the type of query
     */
    public function query($sql)
    {
        return @mysql_query($sql, $this->_link);
    }
    /**
     * Return the id number for the newly added entry
     *
     * @return int
     */
    public function insertID() {
        return @mysql_insert_id();
    }
    /**
     * Get the number of rows in the result
     *
     * @param resource $result The result resource that's being evaluated
     * @return int             The number of rows in the result set, or FALSE on failure
     */
    public function rowCount($result)
    {
        return @mysql_num_rows($result);
    }
    /**
     * Fetch a result row as an associative array
     *
     * @param resource $result     The result resource that's being evaluated
     * @return array[string]string An associative array of strings, or FALSE on failure
     */
    public function fetch($result)
    {
        return mysql_fetch_assoc($result);
    }
    /**
     * Fetch a result row as a numerical array
     *
     * @param resource $result The result resource that's being evaluated
     * @return array           A numerical array of strings, or FALSE if no more rows
     */
    public function fetchRow($result)
    {
        return mysql_fetch_row($result);
    }
    /**
     * Free all memory associated with the result
     *
     * @param resource $result The result resource that's being evaluated
     * @return bool            TRUE on success, or FALSE on failure
     */
    public function free($result)
    {
        return @mysql_free_result($result);
    }
}
